package com.team622.shop.content.service.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.team622.shop.common.exceptions.BusinessException;
import com.team622.shop.common.exceptions.enums.Resp;
import com.team622.shop.content.model.entity.DocContent;
import com.team622.shop.content.service.mapper.DocContentMapper;
import com.team622.shop.content.service.service.DocCategoryService;
import com.team622.shop.content.service.service.DocContentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

/**
 * <p>
 *  文档内容服务实现类
 * </p>
 *
 * @author team622
 * @since 2025-07-16
 */
@Service
public class DocContentServiceImpl extends ServiceImpl<DocContentMapper, DocContent> implements DocContentService {

    @Autowired
    private DocCategoryService docCategoryService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public DocContent addContent(DocContent docContent) {
        // 验证分类是否存在
        if (docContent.getCategoryId() != null && docCategoryService.getCategoryById(docContent.getCategoryId()) == null) {
            throw new BusinessException(Resp.INVALID_PARAM);
        }
        
        // 设置业务默认值
        if (docContent.getStatus() == null) {
            docContent.setStatus((byte) 0); // 默认为草稿状态
        }
        if (docContent.getVersion() == null) {
            docContent.setVersion(1); // 默认版本号为1
        }
        if (docContent.getViewCount() == null) {
            docContent.setViewCount(0); // 默认浏览次数为0
        }
        
        this.save(docContent);
        return docContent;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteContent(Long id) {
        if (id == null) {
            throw new BusinessException(Resp.INVALID_PARAM);
        }
        
        return this.removeById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public DocContent updateContent(DocContent docContent) {
        if (docContent == null || docContent.getId() == null) {
            throw new BusinessException(Resp.INVALID_PARAM);
        }
        
        // 验证分类是否存在
        if (docContent.getCategoryId() != null && docCategoryService.getCategoryById(docContent.getCategoryId()) == null) {
            throw new BusinessException(Resp.INVALID_PARAM);
        }
        
        // 更新版本号
        DocContent existing = this.getById(docContent.getId());
        if (docContent.getContent() != null && !docContent.getContent().equals(existing.getContent())) {
            docContent.setVersion(existing.getVersion() + 1);
        }
        
        this.updateById(docContent);
        return docContent;
    }

    @Override
    public DocContent getContentById(Long id) {
        if (id == null) {
            throw new BusinessException(Resp.INVALID_PARAM);
        }
        
        DocContent docContent = this.getById(id);
        if (docContent != null) {
            docContent.setViewCount(docContent.getViewCount() + 1);
            this.updateById(docContent);
        }
        return docContent;
    }

    @Override
    public Page<DocContent> pageContent(Integer current ,Integer pageSize, String title, Long categoryId, Byte status) {
        LambdaQueryWrapper<DocContent> queryWrapper = new LambdaQueryWrapper<>();
        
        // 按标题模糊查询
        if (StringUtils.hasText(title)) {
            queryWrapper.like(DocContent::getTitle, title);
        }
        
        // 按分类查询
        if (categoryId != null) {
            queryWrapper.eq(DocContent::getCategoryId, categoryId);
        }
        
        // 按状态查询
        if (status != null) {
            queryWrapper.eq(DocContent::getStatus, status);
        }
        
        // 按更新时间降序排序
        queryWrapper.orderByDesc(DocContent::getUpdatedAt);
        
        return this.page(new Page<>(current,pageSize), queryWrapper);
    }
}
